home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / http.rexx < prev    next >
OS/2 REXX Batch file  |  2001-03-01  |  2KB  |  104 lines

  1. /*
  2. http.rexx - A very simple example of a http downloader.
  3. usage: http <url>
  4.  
  5. (Hummmm url ? :)
  6. if output redirected via >file, prints errors and info to stderr
  7. */
  8.  
  9. signal on break_c
  10.  
  11. l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  12.  
  13. prg=ProgramName("NOEXT")
  14.  
  15. if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then
  16.     call err "can't find" result,1
  17.  
  18. if ~RMH_ReadArgs("URL/A,RANGE/K") then do
  19.     call PrintFault(IoErr(),prg)
  20.     exit
  21. end
  22.  
  23. url=parm.0.value
  24.  
  25. if upper(left(url,7))="HTTP://" then parse var url +7 url
  26. p=pos("/",url)
  27. if p=0 then do
  28.     host=url
  29.     file="/"
  30. end
  31. else do
  32.     p=p-1
  33.     parse var url host +p file
  34. end
  35.  
  36. p=pos(":",url)
  37. if p=0 then port=80
  38. else do
  39.     p=p-1
  40.     parse var host host +p dummy +1 port
  41. end
  42.  
  43. if ~Open("STDERR","CONSOLE:","W") then SDTERR="STDOUT"
  44.  
  45. call info prg"/1.0 © alfie"
  46. call info "Host:" host
  47. call info "Port:" port
  48. call info "File:" file
  49.  
  50. call info "resolving host addr..."
  51. sin.addraddr=resolve(host)
  52. if sin.addraddr=-1 then call err "host <"host"> not found",1
  53. sin.addrport=port
  54.  
  55. sock=socket("INET","STREAM")
  56. if sock=-1 then call err "can't create socket"
  57.  
  58. call info "connecting..."
  59. if connect(sock,"SIN")<0 then call err "can't connect"
  60.  
  61. fin="D0A"x
  62. request="GET" space(file) "HTTP/1.0"fin
  63. if parm.1.flag then request=request"Range: bytes" parm1.value||fin
  64. request=request||fin
  65.  
  66. call info "sending request..."
  67. if send(sock,request)<0 then call err "error sending"
  68.  
  69. call info "receiving results..."
  70. if recvline(sock,"BUF",256)<0 then call err "error receiving"
  71. if buf="" then call err "empty answer",1
  72. parse var buf http code
  73. if word(code,1)~=200 then call err "error from server" code,1
  74.  
  75. call info "receiving head..."
  76. do while buf~="D0A"x
  77.     if recvline(sock,"BUF",256)<0 then call err "error receiving"
  78. end
  79.  
  80. call info "receiving file..."
  81. res=recv(sock,"BUF",256)
  82. do while res>0
  83.     call writech("STDOUT",buf)
  84.     res=recv(sock,"BUF",256)
  85. end
  86. if res<0 then call err "error receiving"
  87. call info "done."
  88. exit
  89.  
  90. err:
  91. parse arg msg,ntdoerr
  92.     if ntdoerr~=1 then msg=msg "("errorstring(errno())")"
  93.     call info(msg)
  94.     exit
  95.  
  96. info:
  97. parse arg msg
  98.     call writeln(STDERR,msg)
  99.     return
  100.  
  101. break_c:
  102.     call info "user break."
  103.     exit
  104.